<?php
namespace bee;

function processAjax()
{
  ///die("H E R E 2");
  try
  {
    $app = app::getInstance();
    $cfg = config::getInstance();

    /**
     *
     * datoteka forme - na njoj je smještena funkcija koja obrađuje ajax request
     * @var unknown_type
     */
    $form_page = "";
    $noLoad = isset($_REQUEST["__noload"]) ? $_REQUEST["__noload"] : false;
    if(config::getInstance()->PROCESS_PAGES_FIRST)
    { if(isset($_REQUEST["sys_page"]))
      { $form_page = $_REQUEST["sys_page"];
      } elseif($app->getCurentForm())
      { $form_page = $app->getCurentForm();
      }
    } else
    { if($app->getCurentForm())
      { $form_page = $app->getCurentForm();
      } elseif(isset($_REQUEST["sys_page"]))
      { $form_page = $_REQUEST["sys_page"];
      }
    }
    /**
     *
     * naziv stylesheeta koji se koristi za ajax response
     * @var unknown_type
     */
    $ajaxObjectName              = $_REQUEST["__ajax"] ?: $form_page;//01.10.2010
    //echo "%%$ajaxObjectName%%";
    if(!owns_acces($app->getUser()->id, $form_page))
    {

      processor::getInstance()->denyUser();
    }

    require_once config::getInstance()->sys_USR_APP_PATH . "/$form_page.php";

    $DSDoc = XMLDataHub::getInstance();
    $processor = new    \XSLTProcessor();
    $processor->registerPHPFunctions();
    $styleSheet                = new   \DOMDocument();
    $styleSheet->load(config::getInstance()->sys_TEMPLATE_PATH . "/$ajaxObjectName.xsl");
    $plugins                   = null;
    $disp                      = null;
    if($plugins_class_name     = $cfg->sys_PLUGINS_CLASS)
    { $plugins                 = factory::load("bee_plugins_class");
      if(!$plugins)
      { $plugins               = $plugins_class_name::getInstance();
      }

      if($plugins)
      { $disp                  = dispatcher::getInstance();
        $disp->addObject($plugins);
        $disp->dispatch("preForm");
      }
    }

    $ajaxObject                = call_user_func(array($form_page, "getInstance"), $noLoad);
//die(print_r($ajaxObject));
    if($plugins)
    { $disp->dispatch("postForm");
    }

    $processor->importStylesheet($styleSheet);
    $DSDoc->setVariable(  "sys_page",       $form_page);
    $DSDoc->setVariable(  "__ajax",         $ajaxObjectName);
    $DSDoc->setVariable(  "sys_msg_style", "app::getInstance()->getMessageLevel()");
    $DSDoc->setArray($_REQUEST, "__request");

    if(isset($_REQUEST["__ajax"]) && config::getInstance()->sys_generate_XML)
    { $DSDoc->save(config::getInstance()->sys_TMP_PATH. "/" . $ajaxObjectName . ".xml" );//****
    }


//    echo $processor->transformToXml($DSDoc);


    $t = microtime(true);
    //echo "(pT " . (microtime(true) - $t) . ")";


    $app->echo .= $processor->transformToXml($DSDoc);
    //echo "(PT " . (microtime(true) - $t) . ")";
    if($plugins)
    { $disp->dispatch("postProcess");

    factory::save($plugins, "bee_plugins_class");
    }

    echo $app->echo;

    $ajaxObject->cleanUp();
    $db = db::getInstance();
    $db->cleanUp();

    factory::save($ajaxObject, $ajaxObject::getFormID());
    if($plugins)
    { factory::save($plugins, "bee_plugins_class");
    }
  } catch(Exception $e)
  { echo( "--" - $nodeName . "(xmldatahub setarray exception)/" . print_r($e->getCode(), true));
  }

}
